package com.jyx.healthsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyx.Data_unification.Unification;
import com.jyx.healthsys.entity.Notice;
import com.jyx.healthsys.entity.Role;
import com.jyx.healthsys.service.INoticeService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
class NoticePo{
    private Notice notice;
    private Long pageNo;
    private Long pageSize;
}
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private INoticeService iNoticeService;
    @PostMapping("/list")
    Unification<Map<String,Object>> getNotices(@RequestBody NoticePo noticePo){
        // 构造查询条件
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(noticePo.getNotice().getNoticeName()), Notice::getNoticeName, noticePo.getNotice().getNoticeName());
        wrapper.like(!StringUtils.isEmpty(noticePo.getNotice().getNoticeContent()), Notice::getNoticeContent, noticePo.getNotice().getNoticeContent());
        wrapper.eq(!StringUtils.isEmpty(noticePo.getNotice().getStatus()), Notice::getStatus, noticePo.getNotice().getStatus());
        wrapper.orderByAsc(Notice::getId);

        // 分页查询
        Page<Notice> page = new Page<>(noticePo.getPageNo(), noticePo.getPageSize());
        iNoticeService.page(page, wrapper);

        // 将查询结果封装到Map中返回
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());
        return Unification.success(data);
    }
    @PostMapping("/changeStatus")
    Unification<Integer> changeStatus(@RequestBody Map<String,Integer> map){
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getId, map.get("id"));
        Notice notice = iNoticeService.getOne(wrapper);
        if(notice.getStatus().equals("1")){
            notice.setStatus("0");
        }else{
            notice.setStatus("1");
        }
        LambdaUpdateWrapper<Notice> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Notice::getId, notice.getId())
                .set(Notice::getStatus, notice.getStatus());
        boolean i = iNoticeService.update(updateWrapper);
        if (i){
            return Unification.success();
        }
        return Unification.fail("更新失败");
    }
    @PostMapping("/add")
    Unification<Integer> addNotice(@RequestBody Notice notice){
        notice.setDeleted(0);
        boolean save = iNoticeService.saveOrUpdate(notice);
        if (save){
            return Unification.success();
        }
        return Unification.fail("添加失败");
    }
    @GetMapping("/delete")
    Unification<Integer> deleteNotice(@RequestParam Integer id){
        boolean remove = iNoticeService.removeById(id);
        if (remove){
            return Unification.success();
        }
        return Unification.fail("删除失败");
    }
    @GetMapping("/active-list")
    Unification<List<Notice>> getActiveNotice(){
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getStatus, "1");
        List<Notice> list = iNoticeService.list(wrapper);
        return Unification.success(list);
    }
}
